package test;

import java.util.*;
import java.lang.reflect.*;

public class MappingFromMagento extends MappingBase {
	private Map<String,String> mapFields = new HashMap<String,String>(); 
	private Map<String, Method> mapValues = new HashMap<String, Method>(); 

	
	public MappingFromMagento () {
		init();
	}
	
	private void init () {
		
		
		// Map Fields
		mapFields.put("product-id","sku");
		mapFields.put("online-flag","is_in_stock");
		mapFields.put("display-name","name");
		mapFields.put("long-description","description");
		mapFields.put("category-assignment","_category");
		
		
		mapFields.put("allocation","qty");
		mapFields.put("amount","price");
		
		
		//Map<String,String>online-flag = new HashMap<String,String>();
		//mapValues.put("online-flag", MappingFromMagento.class.map_online_flag("St"));
		
		mapFields.put("magento-custom-attribute","custom-attribute");
		mapFields.put("product-id","sku");

		
		// Map Values
		try {
			//MappingFromMagento.class.getMeth parameterTypes
			//mapValues.put(	"online-flag",  MappingFromMagento.class.getMethod("map_online_flag",String.class));
			mapValues.put(	"category-assignment", this.getClass().getMethod("categoryAssignment",String.class)	);
			mapValues.put(	"online-flag", this.getClass().getMethod("convert2Boolean",String.class)	);
			mapValues.put(	"long-description", this.getClass().getMethod("convert2UpperCase",String.class)	);
			
		} catch (Exception e) {
			System.err.println("Exception MappingFromMagento.java init()");
			System.out.println(e);
		}
	
	
	}
	
	public String mapField (String inField) {
		return mapFields.get(inField);
	}

	public String mapValue (String inField, String value) {
		String ret = "";
		
		try {
			//if(mapValues.containsKey(inField)) mapValues.get(inField).invoke(this,value);
			
			ret= mapValues.containsKey(inField)
				? (String) mapValues.get(inField).invoke(this,value)
				: value;
		} catch (Exception e) {
			System.err.println("Exception MappingFromMagento.java mapValue()");
			System.out.println(e);
		}
		return ret;
	}

	public String mapValue (String inField, CSVRecord record) {
		String v;
		v=record.getFieldValue( mapField(inField) );
		return mapValue(inField, v);
	}

	
	
	/*
	public static String map_online_flag(String v) {
		System.out.println("ha ha ha "+v);
		
		return "1".equalsIgnoreCase(v)
				?	"true"
				:	"false";
	} */
	public String convert2Boolean(String v) {

		//System.out.println("ha ha ha map_online_flag2"+v);
		return "0".equalsIgnoreCase(v)
				?	"false"
				:	"true";
	} 
	

	public String convert2UpperCase(String v) {

		//System.out.println("ha ha ha map_online_flag2"+v);
		return v.toUpperCase();
	} 
	public String categoryAssignment (String v) {    // trim and replace Magento catIds' separators "/" with "-"  
		String cId="",prefix="";
		String[] idBuf;
		int len;
		
		idBuf = v.trim().split("/");
		len=idBuf.length;
		if (len>0) {
			for (int i=0;i<len;i++) {
				cId +=prefix+idBuf[i].trim();
				prefix="-";
			}
		}
		
		return "".equalsIgnoreCase(cId)
				? null
				: cId;
	} 

}
